package cn.apiManager.web.conf;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.SQLLoader;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring.SpringBeetlSql;
import org.beetl.sql.ext.spring.SpringConnectionSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * beetlsql
 * @author Yonggang.Liu
 *
 */
@Configuration
public class BeetlSqlConfig {

	@Resource
	private DataSource dataSource;

	@Bean(name = "sqlManager")
	public SpringBeetlSql GetSpringBeetlSql(@Value("${spring.sql.root}") String sqlRoot){

		SpringBeetlSql sql = new SpringBeetlSql();
		
		SpringConnectionSource cs = new SpringConnectionSource();
		cs.setMasterSource(dataSource);
		sql.setCs(cs);

		sql.setDbStyle(new MySqlStyle());

		SQLLoader sqlLoader = null;
		ClasspathLoader classPath = new ClasspathLoader();
		classPath.setSqlRoot(sqlRoot);
		sql.setSqlLoader(sqlLoader);

		sql.setNc(new UnderlinedNameConversion());

		Interceptor[] interceptors = new Interceptor[] { new DebugInterceptor() };
		sql.setInterceptors(interceptors);
		return sql;
	}
}
